package ch08
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import utils.Ball;
	/**
	 * 根据 Xeno 悖论，缓动的结果永远无法到达准确的位置，只能无限接近.
	 * Flash中，sprite等一次最少移动1/20像素.
	 * 故可以在一个可以接受的范围内，移除 ENTER_FRAME监听。
	 * @author sjp
	 * 
	 */
	public class EasingOff extends Sprite
	{
		private var ball:Ball;
		private var easing:Number=0.2;
		private var targetX:Number=stage.stageWidth / 2;

		public function EasingOff()
		{
			init();
		}

		private function init():void
		{
			ball=new Ball();
			addChild(ball);
			ball.y=stage.stageHeight / 2;
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function onEnterFrame(event:Event):void
		{
			var dx:Number=targetX - ball.x;
			if (Math.abs(dx) < 1)
			{
				ball.x=targetX;
				removeEventListener(Event.ENTER_FRAME, onEnterFrame);
				trace("done");
			}
			else
			{
				var vx:Number=dx * easing;
				ball.x+=vx;
			}
		}
	}
}